package com.demo.designpattern.flyweight;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by hunter
 *
 * @Description 英雄工厂
 */
public class HeroFactory {

    // 享元缓存map
    private Map<HeroEnum, Hero> flyWeightMap = new ConcurrentHashMap<>();

    /**
     * 获取英雄
     *
     * @param heroEnum
     * @return
     */
    public Hero getHero(HeroEnum heroEnum) {
        Hero hero = flyWeightMap.get(heroEnum);
        if (hero == null) {
            switch (heroEnum) {
                case HERO_GALEN:
                    hero = new Galen();
                    break;
                case HERO_EZREAL:
                    hero = new Ezreal();
                    break;
                default:
            }

            flyWeightMap.put(heroEnum, hero);
        }

        return hero;
    }

    /**
     * 查看map
     *
     * @return
     */
    public String getFlyWeightMap() {
        return flyWeightMap.toString();
    }
}
